package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class ShoppingCartServiceImp implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties( shoppingCartDTO,shoppingCart );
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId( userId );
        //一个用户可能点不同口味的菜品
        List<ShoppingCart> list = shoppingCartMapper.getByUserId(shoppingCart);
        //先判断这个数据在购物车表中是否存在
        if( list != null && list.size()>0) {
            //存在number加一即可
            ShoppingCart shoppingCart1 = list.get( 0 );
            shoppingCart1.setNumber( shoppingCart1.getNumber()+1 );
            shoppingCartMapper.update(shoppingCart1);
        } else {
            //不存在 , 判断是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if(dishId != null) {
                Dish dish = dishMapper.getById( dishId );
                shoppingCart.setName( dish.getName() );
                shoppingCart.setImage( dish.getImage() );
                shoppingCart.setAmount( dish.getPrice() );
            } else {
                Setmeal setmeal = setmealMapper.getById( shoppingCart.getSetmealId() );
                shoppingCart.setName( setmeal.getName() );
                shoppingCart.setImage( setmeal.getImage() );
                shoppingCart.setAmount( setmeal.getPrice() );            }
        }
        shoppingCart.setCreateTime( LocalDateTime.now() );
        shoppingCart.setNumber( 1 );
        shoppingCartMapper.insert(shoppingCart);
    }

    /**
     * 查看购物车
     * @return
     */
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder().userId( userId ).build();
        return shoppingCartMapper.list(shoppingCart);
    }

    /**
     * 清空购物车
     */
    public void delete() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder().userId( userId ).build();
        shoppingCartMapper.delete(shoppingCart);
    }

    /**
     * 删除购物车的一个商品
     * @param shoppingCartDTO
     */
    public void deleteOne(ShoppingCartDTO shoppingCartDTO) {
        //假如商品数量大于1 直接修改数据 假如商品数量等于1 直接删除数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties( shoppingCartDTO,shoppingCart );
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId( userId );
        //获取购物车数据
        List<ShoppingCart> list = shoppingCartMapper.list( shoppingCart );
        if(list != null && list.size() >0) {
            shoppingCart = list.get( 0 );
            Integer number = shoppingCart.getNumber();
            if(number == 1) {
                shoppingCartMapper.deleteById(shoppingCart);
            } else {
                shoppingCart.setNumber( shoppingCart.getNumber()-1 );
                shoppingCartMapper.update( shoppingCart );
            }
        }
    }
}
